import request from '@/dvadmin/request/index';

const state = {
	pages: [], // 默认分享数据
	apis: [],
}

const getters = {
	// 判断是否有页面权限
	hasPagePermission:(state)=>(key)=>{
		console.log(key)
		return false
	},
	//判断接口权限
	hasApiPermission:(state)=>(api,method)=>{
		let newMethod=0
		 if(method==='POST'){
			newMethod=1
		}else if(method==='PUT'){
			newMethod=2
		}else if(method==='DETELE'){
			newMethod=3
		}else{
			newMethod = 0
		}
		const api_method = api+":"+newMethod
		const isInArray = state.apis.includes(api_method);
		return isInArray
	},
}

const mutations = {
	setPagePermission(state, pages) {
		state.pages = pages;
	},
	setApis(state, apis) {
		state.apis = apis;
	},
	
}

const actions = {
	// 初始化权限
	async getAppPermission({
		commit,
		dispatch
	}, options) {
		request({
			url: '/api/dvadmin_uniapp/get_app_api_page/',
			method: 'get',
			params: {}
		}).then(res => {
			const {data} = res 
			console.log('permission',data)
			state.pages = data.pages
			state.apis = data.apis
		})
		return false;
	}
}
export default {
	actions,
	getters,
	state,
	mutations
}